<?php
namespace App\Admin\Controllers\Traits\WxSettingTraits;

use App\Admin\Renderable\UsersRender;
use App\Models\User\WxUser;
use App\Wen\Utils\AdminUtils;
use App\Wen\Utils\FieldUtils;
use App\Wen\Utils\GatewayUtils;
use App\Wen\Utils\Settings;
use Dcat\Admin\Form\NestedForm;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Widgets\Form;
use Dcat\Admin\Widgets\Tooltip;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;

trait formUserInviteTrait{
    public function index_userinvite(Content $content)
    {
        return $content->title('分享裂变配置')
//            ->body($this->buildPreviewButton())
//            ->body($this->newline())
            ->body(
                <<<HTML
<div class="card">{$this->form_userinvite()->render()}</div>
HTML
            );
    }

    protected function form_userinvite(){
        $form = new Form();
        $form->confirm('您确定要提交表单吗？', 'content');
        $form->disableResetButton();


        $form->tab('基本', function (Form $form) {
            $form->html(function () {
                return '<div style="background-color: powderblue;color: #414750;padding: 10px;border-radius: 5px;display: inline-block">
场景：假设A邀请B，B邀请C，C邀请D<br/>问题一：什么时候会有这个分成？ 答：D所有发生了<b>微信/支付宝</b>进行支付的情景，比如充值余额，充电，购买商品，开通会员等，A，B和C都能收到对应的奖励，该奖励由系统发放，所以不影响其他人的收益。
<br/>问题二：C为D的一层上级用户，B为D的一层上级用户，A为D的三层上级用户，这种关系会破裂吗？答：永远不会，即使B注销了账号，A还是D的三层上级用户
<br/>问题二：如何才能建立这种关系？答：很简单，<b>分享小程序等任意内容以及二维码都自动带上了你的推广标识</b>，别人点击你的分享并进行了注册，则你们就建立了永久的绑定关系。</div>';
            }, '');
            $res20 = __system__paycode__tip__(20);
            Tooltip::make('.mini_global_admin_open_not_support_tip')
                ->bottom()
                ->title($res20['tip']);
            $form->radio('user_invite_benefit_status', '总开关')->options([0=>'关闭', 1=>'开启'])->help($res20['text'])->default(Settings::get('user_invite_benefit_status', 0));
            $form->html(function () {
                return '';
            }, '<b>一、层级限制</b>');
            $form->radio('user_invite_benefit_max_layer', '最大层级')->options([1=>'一层', 2=>'两层', 3=>'三层'])->default(Settings::get('user_invite_benefit_max_layer', 3));

            $form->html(function () {
                return '';
            }, '<b>二、等级限制</b>');
            $form->radio('app_user_lv_invite_benefit', '我的团队')->options([1=>'lv1', 2=>'lv2', 3=>'lv3', 4=>'lv4', 5=>'lv5', 6=>'lv6'])->help('未达到该等级时，则不能进入该界面，其他不影响')->default(Settings::get('app_user_lv_invite_benefit', 2));

            $form->html(function () {
                return '';
            }, '<b>三、页面公告</b>');
            $form->text('user_invite_page_notice', '公告')->default(Settings::get('user_invite_page_notice', ''));

            $form->html(function () {
                return '';
            }, '<b>四、不显示 我的团队 ?</b>');
            $form->html(function () {
                return '<div style="background-color: powderblue;color: #414750;padding: 10px;border-radius: 5px;display: inline-block">有且仅有这几种原因，不用怀疑是不是bug。<br /> 1、开启了审核模式<br /> 2、[后台-移动端-我的-左侧栏]未添加 我的团队<br /> 3、等级限制<br /> 4、模块未启用<br /> 5、删除缓存，重新登录试一下</div>';
            }, '');

        });

        $form->tab('邀请即时收益', function (Form $form) {
            $form->html(function () {
                return '<div style="background-color: powderblue;color: #414750;padding: 10px;border-radius: 5px;display: inline-block">即时收益只有你直接邀请的人才有奖励，且需要你在白名单，才会获得即时到账</div>';
            }, '');
            $form->checkbox('user_invite_immediately_benefit_white_list', '邀请人白名单')->options(FieldUtils::userPermissions('', true))->when('in', 10, function (Form $form){
                $form->multipleSelectTable('user_invite_immediately_benefit_white_list_users', '自定义用户列表')
                    ->title('用户')
                    ->max(10000)
                    ->from(UsersRender::make())
                    ->model(WxUser::class, 'id', 'user_name', 'selectTable:formUserInviteTrait:white_list_users2')
                    ->default(Settings::get('user_invite_immediately_benefit_white_list_users', ''));
            })->default(Settings::get('user_invite_immediately_benefit_white_list', ''));
            $form->divider();

            $form->number('user_invite_immediately_benefit_coins', '金币奖励 / 位')->default(Settings::get('user_invite_immediately_benefit_coins', 0));
            $form->decimal('user_invite_immediately_benefit_blance', '余额奖励 / 位')->default(Settings::get('user_invite_immediately_benefit_blance', 0.0));
            $form->switch('user_invite_immediately_benefit_auto_withdrawal', '自动微信打款')->help('请先开启[全局配置-支付-微信-商户《微信转账到零钱》的开关]')->default(Settings::get('user_invite_immediately_benefit_auto_withdrawal', 0));
        });

        $form->tab('订单分成', function (Form $form) {

            $form->html(function () {
                return '';
            }, '<b>一、普通用户</b>');
            $form->html(function () {
                return '<div style="background-color: powderblue;color: #414750;padding: 10px;border-radius: 5px;display: inline-block">注意：所有订单的分成，只有在微信，支付宝的钱流入系统才有效，余额支付这种站内流通是不会有收益的</div>';
            }, '');
            $form->table('user_invite_benefit_order_rate_1','普通类订单&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;', function (NestedForm $table) {
                $table->select('type', '订单类型')->options([1=>'会员订单', 2 =>'充电订单', 3=>'余额充值', 6=>'笔记推广',
                    7=>'加入付费圈子', 8=>'保证金充值', 10=>'自助解封解禁', 11=>'付费联系方式']);
                $table->decimal('lv1', '一层上级用户分成/%')->help('也就是直接邀请人，理应是最多的收益');
                $table->decimal('lv2', '二层上级用户分成/%');
                $table->decimal('lv3', '三层上级用户分成/%');
            })->default(Settings::get('user_invite_benefit_order_rate_1', ''));

            $form->table('user_invite_benefit_shop_order_rate_1','商城类订单&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;', function (NestedForm $table) {
                // todo：待细分：扫码核销，虚拟物品
                $table->select('type', '订单类型')->options([0=>'普通商城订单',
                    6=>'闲置买卖订单', 7=>'组局订单', ]);
                $table->decimal('lv1', '一层上级用户分成/%')->help('也就是直接邀请人，理应是最多的收益');
                $table->decimal('lv2', '二层上级用户分成/%');
                $table->decimal('lv3', '三层上级用户分成/%');
            })->default(Settings::get('user_invite_benefit_shop_order_rate_1', ''));

            $form->divider();

            $form->html(function () {
                return '';
            }, '<b>二、推广专员</b>');

            $form->html(function () {
                return '<div style="background-color: powderblue;color: #414750;padding: 10px;border-radius: 5px;display: inline-block">注意：所有订单的分成，只有在微信，支付宝的钱流入系统才有效，余额支付这种站内流通是不会有收益的</div>';
            }, '');
            $form->table('user_invite_benefit_order_rate_2','普通类订单&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;', function (NestedForm $table) {
                $table->select('type', '订单类型')->options([1=>'会员订单', 2 =>'充电订单', 3=>'余额充值', 6=>'笔记推广',
                    7=>'加入付费圈子', 8=>'保证金充值', 10=>'自助解封解禁', 11=>'付费联系方式']);
                $table->decimal('lv1', '一层上级用户分成/%')->help('也就是直接邀请人，理应是最多的收益');
                $table->decimal('lv2', '二层上级用户分成/%');
                $table->decimal('lv3', '三层上级用户分成/%');
            })->default(Settings::get('user_invite_benefit_order_rate_2', ''));

            $form->table('user_invite_benefit_shop_order_rate_2','商城类订单&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;', function (NestedForm $table) {
                // todo：待细分：扫码核销，虚拟物品
                $table->select('type', '订单类型')->options([0=>'普通商城订单',
                    6=>'闲置买卖订单', 7=>'组局订单', ]);
                $table->decimal('lv1', '一层上级用户分成/%')->help('也就是直接邀请人，理应是最多的收益');
                $table->decimal('lv2', '二层上级用户分成/%');
                $table->decimal('lv3', '三层上级用户分成/%');
            })->default(Settings::get('user_invite_benefit_shop_order_rate_2', ''));

            $form->divider();

            $form->html(function () {
                return '';
            }, '<b>三、合伙人</b>');
            $form->html(function () {
                return '<div style="background-color: powderblue;color: #414750;padding: 10px;border-radius: 5px;display: inline-block">注意：所有订单的分成，只有在微信，支付宝的钱流入系统才有效，余额支付这种站内流通是不会有收益的</div>';
            }, '');
            $form->table('user_invite_benefit_order_rate_3','普通类订单&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;', function (NestedForm $table) {
                $table->select('type', '订单类型')->options([1=>'会员订单', 2 =>'充电订单', 3=>'余额充值', 6=>'笔记推广',
                    7=>'加入付费圈子', 8=>'保证金充值', 10=>'自助解封解禁', 11=>'付费联系方式']);
                $table->decimal('lv1', '一层上级用户分成/%')->help('也就是直接邀请人，理应是最多的收益');
                $table->decimal('lv2', '二层上级用户分成/%');
                $table->decimal('lv3', '三层上级用户分成/%');
            })->default(Settings::get('user_invite_benefit_order_rate_3', ''));

            $form->table('user_invite_benefit_shop_order_rate_3','商城类订单&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;', function (NestedForm $table) {
                // todo：待细分：扫码核销，虚拟物品
                $table->select('type', '订单类型')->options([0=>'普通商城订单',
                    6=>'闲置买卖订单', 7=>'组局订单', ]);
                $table->decimal('lv1', '一层上级用户分成/%')->help('也就是直接邀请人，理应是最多的收益');
                $table->decimal('lv2', '二层上级用户分成/%');
                $table->decimal('lv3', '三层上级用户分成/%');
            })->default(Settings::get('user_invite_benefit_shop_order_rate_3', ''));

        });


        $form->action('invite_benefit/action/config');
        return $form;
    }

    public function userinvite(Request $request){

        if($request->input('user_invite_immediately_benefit_auto_withdrawal') == 1){
            if(Settings::get('mch_transfer_to_weChat', 0) != 1){
                return $this->response()->error('请先开启[全局配置-支付-微信-商户《微信转账到零钱》的开关]');
            }
        }


        _forget_config_data();

        $user_invite_immediately_benefit_white_list = $request->input('user_invite_immediately_benefit_white_list');
        $user_invite_immediately_benefit_white_list_ = [];
        if($user_invite_immediately_benefit_white_list){
            foreach ($user_invite_immediately_benefit_white_list as $i){
                if($i === null){
                    continue;
                }
                $user_invite_immediately_benefit_white_list_[] = (int)$i;
            }
        }
        Settings::set('user_invite_immediately_benefit_white_list', $user_invite_immediately_benefit_white_list_, true);
        Settings::set('user_invite_immediately_benefit_white_list_users', $request->input('user_invite_immediately_benefit_white_list_users'), true);


//        Settings::set('user_invite_benefit_lv1_rate', _between_($request->input('user_invite_benefit_lv1_rate'), 0, 100), true);
//        Settings::set('user_invite_benefit_lv2_rate', _between_($request->input('user_invite_benefit_lv2_rate'), 0, 100), true);
//        Settings::set('user_invite_benefit_lv3_rate', _between_($request->input('user_invite_benefit_lv3_rate'), 0, 100), true);

        Settings::set('user_invite_benefit_order_rate_1', AdminUtils::_table_data($request->input('user_invite_benefit_order_rate_1')), true);
        Settings::set('user_invite_benefit_shop_order_rate_1', AdminUtils::_table_data($request->input('user_invite_benefit_shop_order_rate_1')), true);

        Settings::set('user_invite_benefit_order_rate_2', AdminUtils::_table_data($request->input('user_invite_benefit_order_rate_2')), true);
        Settings::set('user_invite_benefit_shop_order_rate_2', AdminUtils::_table_data($request->input('user_invite_benefit_shop_order_rate_2')), true);

        Settings::set('user_invite_benefit_order_rate_3', AdminUtils::_table_data($request->input('user_invite_benefit_order_rate_3')), true);
        Settings::set('user_invite_benefit_shop_order_rate_3', AdminUtils::_table_data($request->input('user_invite_benefit_shop_order_rate_3')), true);

        Settings::set('user_invite_benefit_status', _between_((int)($request->input('user_invite_benefit_status')), 0, 100), true);
        Settings::set('app_user_lv_invite_benefit', $request->input('app_user_lv_invite_benefit'), true);
        Settings::set('user_invite_benefit_max_layer', $request->input('user_invite_benefit_max_layer'), true);

        Settings::set('user_invite_immediately_benefit_auto_withdrawal', $request->input('user_invite_immediately_benefit_auto_withdrawal') == 1 ? 1 : 0, true);


        Settings::set('user_invite_immediately_benefit_coins', _between_((int)($request->input('user_invite_immediately_benefit_coins')), 0, 1000000), true);
        Settings::set('user_invite_immediately_benefit_blance', _between_($request->input('user_invite_immediately_benefit_blance'), 0, 1000000), true);

        Settings::set('user_invite_page_notice', $request->input('user_invite_page_notice'), true);


        $__global_config_version__ = Settings::get('__global_config_version__', 1) + 1;
        Settings::set('__global_config_version__', $__global_config_version__);
        GatewayUtils::success('all', 13, ['version' => $__global_config_version__]);
        return $this->response()->success('成功！');
    }

}
